<?php
/**
 * Crc CMS
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage Puddle
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: adminController.php 54 2012-10-02 15:41:20Z mcalin45@gmail.com $
 */

/**
 * Puddle admin controller
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage Puddle
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 */
class Puddle_AdminController extends Crc_Controller_Action
{
    protected $doctrine;
    protected $em;
    protected $puddleRepository;



    public function init()
    {

         parent::init();
         $this->userRepository = $this->entityManager->getRepository('\Crc\Entity\Puddle');
    }

    /**
     * Display puddle list
     *
     * @return void
     */
    public function indexAction()
    {
        //\Zend_Debug::dump($this->getRequest()->getPost()); exit;
        $grid = new Crc\Grid('\Crc\Entity\Puddle');
        //$grid->resetFilters();
        //$grid->resetColumns();

        if ($this->_getParam('quick_action')) {
            try {
                $grid->setQuickAction($this->_getParam('quick_action'), $this->_getParam('action_item'));
            } catch (Exception $e) {
                 Zend_Debug::dump($e);

            }
            
            $this->entityManager->flush();
            
            $this->_redirect('/puddle/admin/');
            
            exit;
        }

        if ($this->_getParam('do_filter') == 1) {
            if ($this->_getParam('reset_filter') == 2) {
                $grid->resetFilters();
            } else {
                $grid->resetFilters();
                $postData = $this->_getParam('filter');
                if (isset($postData['fields']) && is_array($postData['fields']) && count($postData['fields']) > 0) {
                    foreach($postData['fields'] as $filter) {
                        $grid->selectFilter(
                            $filter,
                            $postData['operators'][$filter],
                            $postData['values'][$filter]
                        );
                    }
                }
            }

            $selectedColumns = $this->_getParam('options');
            $selectedColumns = $selectedColumns['selected_columns'];
            $grid->setVisibleColumns($selectedColumns);

            $this->_redirect('/puddle/admin/');
        }
        if ($this->_getParam('set') == 'order') {
            $grid->setOrder($this->_getParam('field'));
            $this->_redirect('/puddle/admin/');
        }
        $queryBuilder = $grid->processGrid();

    

        $this->view->grid = $grid;

        $adapter = new Crc_Paginator_Adapter_Doctrine($queryBuilder);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setItemCountPerPage(10);
        $this->view->paginator = $paginator;
    
    }

    /**
     * Edit a puddle
     *
     * @return void
     */
    public function saveAction()
    {
        $adapter = new CU_Form_Model_Adapter_Doctrine2($this->em);
        $form = new CU_Form_Model(array(
            'adapter' => $adapter,
            'model' => 'Crc\Entity\Puddle',
            'ignoreColumns' => array('sort_order', 'status')
        ));
        $element = new Zend_Form_Element_Submit('Trimite');
        $form->addElement($element);

        $itemId = $this->getRequest()->getParam('id');
        if ($itemId > 0) {
            $puddle = $this->puddleRepository->find($itemId);
            $form->setRecord($puddle);
        } else {
            $puddle = new Crc\Entity\Puddle();
        }


        if ($this->getRequest()->isPost())
        {
            if ($form->isValid($this->getRequest()->getPost()))
            {
                $puddle = $form->save();
                $puddle->setStatus(1);
                $this->entityManager->flush();
                $this->_redirect('/puddle/admin/');
            }
        }

        $this->view->form = $form;
    }

    /**
     * Delete a puddle
     *
     * @return void
     */
    public function deleteAction()
    {
        $itemId = $this->getRequest()->getParam('id');
        $puddle = $this->puddleRepository->find($itemId);
        $puddle->setStatus(Crc\Entity\Puddle::STATUS_DELETED);
        $this->entityManager->flush();
        $this->_redirect('/puddle/admin/');
    }

    function postDispatch()
    {
        $this->entityManager->flush();
    }
}